package com.rrd.dw.mr.key;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.WritableComparable;

public class IntPair  implements WritableComparable<IntPair>{
    private String first;
    private int second;
    
    public IntPair(){
    }
    
    public IntPair(String left, int right){
        set(left, right);
    }
    
    public void set(String left, int right){
        first = left;
        second = right;
    }
    
    @Override
    public void readFields(DataInput in) throws IOException{
        first = in.readUTF();
        second = in.readInt();
    }
    
    @Override
    public void write(DataOutput out) throws IOException{
        out.writeUTF(first);
        out.writeInt(second);
    }
    
    @Override
    public int compareTo(IntPair o)
    {
        if (!first.equals(o.first)){
            return first.compareTo(o.first)>0  ? -1 : 1;
        }else if (second != o.second){
            return second < o.second ? -1 : 1;
        }else{
            return 0;
        }
    }
    
    @Override
    public int hashCode(){ 
        return (int) (first.hashCode() * 127 + this.second);
    }
    
    @Override
    public boolean equals(Object right){
        if (right == null)
            return false;
        if (this == right)
            return true;
        if (right instanceof IntPair){
            IntPair r = (IntPair) right;
            return r.first.equals(first) && r.second == second;
        }else{
            return false;
        }
    }
    
    public String getFirst(){
        return first;
    }
    
    public int getSecond(){
        return second;
    }
}
